package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.BookMapper;
import com.example.demo.dao.BookShelfMapper;
import com.example.demo.dao.ScoreMapper;
import com.example.demo.model.Book;
import com.example.demo.model.BookShelf;
import com.example.demo.model.User;

import com.example.demo.service.InterestingService;
import com.example.demo.service.UserFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/5/20 12:43
 */
@Controller
public class HomepageController {

    @Autowired(required = false)
    private BookMapper bookMapper;
     @Autowired(required = false)
    private BookShelfMapper bookShelfMapper;
    @Autowired(required = false)
    private ScoreMapper scoreMapper;
private InterestingService interestingService;
    @GetMapping("home")
    public String homepage(HttpServletResponse response, HttpServletRequest request, Model model, HttpSession session) {
        ////////////////////////////////////////////
        //如何保证推荐的小说始终有四个，如何实现可以换一换
        //首页只展示固定的四本书，所有推荐都展示在个人主页
        User user = (User) session.getAttribute("user");
        model.addAttribute("user", user);

        //实现皮尔逊推荐
        ArrayList<Integer> Pearson_recBookidList = new ArrayList<>();
         if (scoreMapper.countuser()>=5&&bookShelfMapper.countuser()>=5) {
            //bookid__感兴趣程度
            List<Map.Entry<Integer, Double>> pearson_rec = interestingService.pearson_rec(user.getId());
            for (Map.Entry<Integer, Double> entry : pearson_rec) {
                Pearson_recBookidList.add(entry.getKey());
            }

            for (Integer integer : Pearson_recBookidList) {
                System.out.println("推荐小说id:" + integer);
            }
            List<Book> Pearson_recList = new ArrayList<Book>();

            if (Pearson_recBookidList.size() != 0) {
                Pearson_recList = bookMapper.selectList(new QueryWrapper<Book>().in("id", Pearson_recBookidList));
            }
            System.out.println("皮尔逊推荐小说数量：" + Pearson_recList.size());


            //实现杰卡德推荐
            ArrayList<Integer> Jkd_recBookidList = new ArrayList<>();
            if (user != null) {
                //bookid__感兴趣程度
                List<Map.Entry<Integer, Double>> jkd_rec = interestingService.jkd_rec(user.getId());

                for (Map.Entry<Integer, Double> entry : jkd_rec) {
                    Jkd_recBookidList.add(entry.getKey());
                }

                for (Integer integer : Jkd_recBookidList) {
                    System.out.println("推荐小说id:" + integer);
                }
                List<Book> Jkd_recList = new ArrayList<Book>();
                if (Jkd_recBookidList.size() != 0) {
                    Jkd_recList = bookMapper.selectList(new QueryWrapper<Book>().in("id", Jkd_recBookidList));
                }

                System.out.println("jkd推荐小说数量：" + Jkd_recList.size());

//Pearson_recList、Jkd_recList 对集合进行去重
                List<Book> recList = new ArrayList<Book>();
                recList.addAll(Jkd_recList);
                List<Book> content_based = interestingService.Content_based(user.getId());
                for (Book book : Pearson_recList) {
                    if (!recList.contains(book))
                    {
                        recList.add(book);
                    }

                }
                for (Book book : content_based) {
                    if (!recList.contains(book))
                    {
                        recList.add(book);
                    }
                }
                model.addAttribute("recList", recList);
            }
        }
        return "homepage";
    }
}
